package com.brotherly.util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.widget.Toast;

public class ContactUtil
{
    // the selected cols for contact users
    String[] selectCol = new String[]
    {
            ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER,
            ContactsContract.Contacts._ID
    };
    public static final int COL_NAME = 0;
    public static final int COL_HAS_PHONE = 1;
    public static final int COL_ID = 2;

    // the selected cols for phones of a user
    String[] selPhoneCols = new String[]
    {
            ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.TYPE
    };
    public static final int COL_PHONE_NUMBER = 0;
    public static final int COL_PHONE_NAME = 1;
    public static final int COL_PHONE_TYPE = 2;
    private Activity mActivity;

    public ContactUtil(Activity activity)
    {
        this.mActivity = activity;
    }

    public List<ContactItemData> getContactList()
    {
        String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                + Contacts.DISPLAY_NAME + " != '' ))";

        List<ContactItemData> list = new ArrayList<ContactItemData>();
        Cursor cursor = mActivity.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, selectCol, select, null,
                ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
        if (cursor == null)
        {
            Toast.makeText(mActivity, "cursor is null!", Toast.LENGTH_LONG).show();
            return list;
        }
        if (cursor.getCount() == 0)
        {
            Toast.makeText(mActivity, "cursor count is zero!", Toast.LENGTH_LONG).show();
            return list;
        }

        cursor.moveToFirst();
        while (!cursor.isAfterLast())
        {
            int contactId;
            contactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            if (cursor.getInt(COL_HAS_PHONE) > 0)
            {
                // the contact has numbers
                // 获得联系人的电话号码列表
                String displayName;
                displayName = cursor.getString(COL_NAME);
                Cursor phoneCursor = mActivity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        selPhoneCols,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
                if (phoneCursor.moveToFirst())
                {
                    do
                    {
                        // 遍历所有的联系人下面所有的电话号码
                        String phoneNumber = phoneCursor.getString(COL_PHONE_NUMBER);
                        if (phoneNumber != null && !"".equals(phoneNumber))
                        {
                            ContactItemData data = new ContactItemData();
                            data.name = displayName;
                            data.number = phoneNumber;
                            data.check = false;
                            list.add(data);
                        }
                    } while (phoneCursor.moveToNext());
                }
                phoneCursor.close();
            }
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }
}
